package com.example.restatr.Atcivity.GarbageClassification;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

public class GarbageClassificationAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private Banner rbt;
    private GridView card;
    private TabLayout Tab;
    public static ViewPager vp;
    private List<String> tabTitle = new ArrayList<>();
    public static List<String> tabId = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private List<GarbageClassBean.RowsBean> rowsBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_garbage_classification_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        rbt = findViewById(R.id.banner);
        card = findViewById(R.id.card);
        Tab = findViewById(R.id.Tab);
        vp = findViewById(R.id.vp);

        Tools.SetBarTitle(toolbar, barTitle, "垃圾分类", search, this, false);
        Tools.SetBanner("/prod-api/api/garbage-classification/ad-banner/list", MainActivity.sp.getString("token", ""),
                "data", "imgUrl",rbt,this);
        Tools.GetXwFl("/prod-api/api/garbage-classification/news-type/list", MainActivity.sp.getString("token", ""), this, "rows", tabTitle, tabId, fragments, vp, Tab, getSupportFragmentManager());
        MainActivity.info = "lj";
        Tab.setTabMode(TabLayout.MODE_FIXED);
        GetGarbageType();
    }

    private void GetGarbageType() {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/garbage-classification/garbage-classification/list", MainActivity.sp.getString("token", ""));
            GarbageClassBean garbageClassBean = new Gson().fromJson(token, GarbageClassBean.class);
            rowsBeans.addAll(garbageClassBean.getRows());
            runOnUiThread(() -> {
                if (rowsBeans.size() > 0) {
                    card.setAdapter(new adapter());
                }
            });
        }).start();
    }

    class adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return rowsBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return rowsBeans.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.card_item_layout, null);
            ImageView icon = convertView.findViewById(R.id.icon);
            TextView text = convertView.findViewById(R.id.text);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(75, 75);
            icon.setLayoutParams(layoutParams);
            Glide.with(GarbageClassificationAt.this)
                    .load(Tools.ip + rowsBeans.get(position).getImgUrl())
                    .into(icon);
            text.setText(rowsBeans.get(position).getName());
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(GarbageClassificationAt.this, GarbageInfoAt.class);
                    intent.putExtra("data", new Gson().toJson(rowsBeans.get(position)));
                    startActivity(intent);
                }
            });
            return convertView;
        }
    }

}